
package com.hjm.common.utils.trade;

import com.alibaba.fastjson.JSONObject;
import com.hjm.common.utils.LogUtils;
import com.hjm.common.utils.request.AutoLoadData;
import lombok.val;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Socket;

public class TranddleUtil {
	
	private static Socket socket;
	private static OutputStream os;
	private static InputStream is;

		
	public static String asciiToString(byte[] value) throws UnsupportedEncodingException {
		return new String(value, "utf-8");
	}

	/** type:0-交易推送、1-代付推送；settletype:0-D0、1-T1 **/
	public static String test(String serial,int type,String settletype,
							  String settleMethod,String amount,String msgtype){
		val payMap = new JSONObject();
		switch (type){
			case 0: //线下
				//交易推送--交易未清算的
				payMap.put("serial", serial);	//交易流水号
				payMap.put("pattern", settletype);	//0 D0  1 T1
				payMap.put("msgType", msgtype); //流水号
				payMap.put("sourcetype","0");//0线下 1线上
				break;
			case 1://线上
				//交易推送--交易未清算的
				payMap.put("serial", serial);	//交易流水号
				payMap.put("pattern", settletype);	//0 D0  1 T1
				payMap.put("msgType", msgtype); //流水号
				payMap.put("sourcetype","1");//0线下 1线上
				break;
			case 4:
				//重新入账
				payMap.put("serial", serial);		//交易流水号
				payMap.put("pattern", settleMethod);	//0 D0  1 T1
				payMap.put("msgType", msgtype); 	//流水号
				payMap.put("sourcetype","4");		// 重新入账
				break;
			default:break;
		}
		return payMap.toJSONString();
	}


	/** type:0-交易推送、1-代付推送；settletype:0-D0、1-T1 **/
	public static String testBack(String serial,int type,String settletype,
							  String settleMethod,String amount,String msgtype){
		val payMap = new JSONObject();
		switch (type){
			case 0:
				//交易推送--交易未清算的
				payMap.put("serial", serial);	//交易流水号
				payMap.put("pattern", settletype);	//0 D0  1 T1
				payMap.put("amount", amount);
				payMap.put("msgType", msgtype); //流水号
				payMap.put("sourcetype","0");//0线下 1线上
				break;
			case 1:
				//代付推送--单笔交易
				payMap.put("txnNo", serial);	//流水号
				payMap.put("settleMethod", settleMethod);	//结算方式00:T+0  01:T+N(N>0) 02:收入费用结转 03:其他
				payMap.put("settleType", "00");		//00 商户结算 01 代理结算
				payMap.put("txnType", "02");	//02 单笔交易  05 单笔查询
				break;
			case 2:
				//代付推送--单笔查询
				payMap.put("txnNo", serial);	//流水号
				payMap.put("settleMethod", settleMethod);	//结算方式00:T+0  01:T+N(N>0) 02:收入费用结转 03:其他
				payMap.put("settleType", "00");		//00 商户结算 01 代理结算
				payMap.put("txnType", "05");	//02 单笔交易  05 单笔查询
				break;
			case 4:
				//交易推送--交易未清算的
				payMap.put("serial", serial);	//交易流水号
				payMap.put("pattern", settletype);	//0 D0  1 T1
				payMap.put("amount", amount);
				payMap.put("msgType", msgtype); //流水号
				payMap.put("sourcetype","4");//0线下 1线上
				break;
			case 5:
				//交易推送--交易未清算的
				payMap.put("serial", serial);	//交易流水号
				payMap.put("pattern", settletype);	//0 D0  1 T1
				payMap.put("msgType", msgtype); //流水号
				payMap.put("sourcetype","5");//0线下 1线上
				break;
			case 6:
				//交易推送--交易未清算的
				payMap.put("serial", serial);	//交易流水号
				payMap.put("pattern", settletype);	//0 D0  1 T1
				payMap.put("msgType", msgtype); //流水号
				payMap.put("sourcetype","6");//0线下 1线上
				break;
			default:break;
		}
		return payMap.toJSONString();
	}

	/** 流水号、方法类型(0-交易推送、1-代付推送；settletype:0-D0、1-T1)、商户类型：2-机构商户、其它-钱宝商户 4 重新入账**/
	public static void send(String serial,int type,String settletype,Integer mertype,
							String settleMethod,String amount,String msgtype) throws Exception {
		try {
			LogUtils.logInfo("请求地址:" + AutoLoadData.vmAccountIp);
			LogUtils.logInfo("请求端口:" + AutoLoadData.vmAccountPort);
			String data = TranddleUtil.test(serial,type,settletype,settleMethod,amount,msgtype);
			LogUtils.logInfo("请求参数:" + data);

			//115.182.230.164 7000    "10.10.70.107",15502
			socket = new Socket(AutoLoadData.vmAccountIp,AutoLoadData.vmAccountPort);
			os = socket.getOutputStream();
			is = socket.getInputStream();
			byte[] by = data.getBytes("utf-8");
			String len = "0000"+by.length;
			len = len.substring(len.length()-4);
			byte[] lenlen = len.getBytes("utf-8");
			byte[] pack = new byte[4+by.length];
			System.arraycopy(lenlen, 0, pack, 0, 4);
			System.arraycopy(by, 0, pack, 4, by.length);
			System.out.println("请求包:"+asciiToString(pack));
			os.write(pack);
			os.flush();
		} catch (IOException e) {
			throw e;
		}finally {
			if (is!=null){is.close();}
			if (os!=null){os.close();}
			if (socket!=null){socket.close();}
		}
	}




	/** 流水号、方法类型(0-交易推送、1-代付推送；settletype:0-D0、1-T1)、商户类型：2-机构商户、其它-钱宝商户 4 重新入账**/
	public static void sendInputMoney(String serial,int type,String settletype,Integer mertype,
									  String settleMethod,String amount,String msgtype) throws Exception {
		try {
			LogUtils.logInfo("请求地址:" + AutoLoadData.inputMoneyIp);
			LogUtils.logInfo("请求端口:" + AutoLoadData.inputMoneyPort);
			String data = TranddleUtil.test(serial,type,settletype,settleMethod,amount,msgtype);
			LogUtils.logInfo("请求参数:" + data);
			//115.182.230.164 7000    "10.10.70.107",15502
			socket = new Socket(AutoLoadData.inputMoneyIp,AutoLoadData.inputMoneyPort);
			os = socket.getOutputStream();
			is = socket.getInputStream();
			byte[] by = data.getBytes("utf-8");
			String len = "0000"+by.length;
			len = len.substring(len.length()-4);
			byte[] lenlen = len.getBytes("utf-8");
			byte[] pack = new byte[4+by.length];
			System.arraycopy(lenlen, 0, pack, 0, 4);
			System.arraycopy(by, 0, pack, 4, by.length);
			System.out.println("请求包:"+asciiToString(pack));
			os.write(pack);
			os.flush();
		} catch (IOException e) {
			throw e;
		}finally {
			if (is!=null){is.close();}
			if (os!=null){os.close();}
			if (socket!=null){socket.close();}
		}
	}




}
